package yxy.game.pm2.module.tip.checker;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import yxy.apple.util.PubSub.MethodSubscriber;
import yxy.apple.util.PubSub.SubscriptionMethod;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.debris;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.pubsub.CurrencyPublisher;
import yxy.game.pm2.bean.pubsub.CurrencyPublisher.CurrencySubscription;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

public class TipSubscriber_碎片兑换 extends MethodSubscriber {

	static private TipSubscriber_碎片兑换 instance;

	public static TipSubscriber_碎片兑换 getInstance() {
		if (instance == null) {
			instance = new TipSubscriber_碎片兑换();
			instance.init();
		}
		return instance;
	}

	private void init() {
		CurrencyPublisher.getInstance().subscribe(this);
	}

	private TipSubscriber_碎片兑换() {
	}

	@SubscriptionMethod(Type = CurrencySubscription.class)
	public void receive(CurrencySubscription subscription) {
		String playerCode = subscription.getPlayerCode();
		if (!subscription.checkTypes(allowType())) {
			return;
		}
		new TipHandle().updateTip(playerCode, TipType.背包_碎片切页, check(playerCode));
	}

	private boolean check(String playerCode) {
		Collection<Currency> currencies = Item.all(playerCode, CurrencyType.CT_碎片).values();
		for (Currency currency : currencies) {
			long number = currency.getNumber();
			debris data = DataCenter.getData(currency.getDataId(), debris.class);
			if (number >= data.getSynthesis()) {
				return true;
			}
		}

		return false;
	}

	private Set<CurrencyType> allowTypes;

	private Set<CurrencyType> allowType() {
		if (allowTypes != null) {
			return allowTypes;
		}
		allowTypes = new HashSet<>();

		allowTypes.add(CurrencyType.CT_碎片);

		return allowTypes;
	}

}
